/*=========  登录表单提交检测  ========*/
	var oEmail = document.getElementById("email");
	var oPassword = document.getElementById("password");
	var oLoginBtn =  document.getElementById("login-btn");
	var oEmailTip = document.getElementById("email-tip");
	var oLoginTip = document.getElementById("login-tip");
	var oPasswordTip = document.getElementById("password-tip");
	oEmail.focus();
	oEmail.onkeyup = function(){checkEmail(oEmail,oEmailTip);};
	oEmail.onblur = function(){checkEmail(oEmail,oEmailTip);};
	oPassword.onkeyup = function(){checkPassword(oPassword,oPasswordTip);};
	oPassword.onkeydown = function(e){
		e = e || event;
		if(e.keyCode == 13){
			login();
		}
	}
	clearBubble(oEmail,"mousedown");
	clearBubble(oPassword,"mousedown");
	clearBubble(oLoginBtn,"mousedown");
	oLoginBtn.onclick = login;
	function login(){
		if(checkEmail(oEmail,oEmailTip) && checkPassword(oPassword,oPasswordTip)){
			ajax({
				type : "post",
				url : "/cyl/login",
				data : {
					"email" : oEmail.value,
					"password" : oPassword.value
				},
				success : requestSuccess,
				error : requestError
			});
			//requestSuccess('{"status":1,"useremail":"742230063@qq.com","img_url":"img/people.jpg"}')

		}
	}
	
	function logout(){
		delCookieAll();
		window.location.href = "index.html";
	}
/*=============== 登录成功或失败 ================*/
	var userarea = document.getElementById("userarea");
	var registerBtn = document.getElementById("register");
	var shopCarNum = document.getElementById("shopcar-num");
	var newsNum = document.getElementById("news-num");
	var oNews = document.getElementById("news");
	var oUserhead = document.getElementById("userhead");
	function requestSuccess(response){
		console.log(response);
		var userInfor = JSON.parse(response);
		if(userInfor.status == "0"){
			oEmail.focus();
			oLoginTip.innerHTML = "邮箱或密码错误哟";
		}else if(userInfor.status == "-1"){
            oEmail.focus();
			oLoginTip.innerHTML = "您还没有注册";
		}
		else {
			loginSuccess(userInfor);
			askInfor(userInfor);
		}
	}
	function loginSuccess(userInfor){
		//console.log(userInfor.useremail+"   "+userInfor.img_url);
		setCookie("useremail",userInfor.email);
		setCookie("img_url",userInfor.image);
		logined();
	}
	//设置购物车以及消息数

	function setNumber(response){
		var number = JSON.parse(response);
		if(number.shopcar > 0){
			shopCarNum.style.background = "#f00";
			shopCarNum.innerHTML = number.shopcar;
		}
		if(number.message > 0){
			newsNum.style.background = "#f00";
			newsNum.innerHTML = number.message;
		}
	}
	function requestError(){
		oLoginTip.innerHTML = "访问服务器失败";
	}

	//请求购物车和消息数量信息

	function askInfor(userInfor){
		/*
		ajax({

				type : "post",

				url : "/login/",

				data : {

					"email" : userInfor.email,

				},

				success : setNumber,

		});*/
		console.log(2);
		setNumber('{"shopcar":2,"message":3}');
	}
/*============ 弹出登录框 ==============*/
	var oLoginBtn = document.getElementById("login");
	var oLoginBox = document.getElementById("login-box");
	var oLoginClose = document.getElementById("login-close");
	oLoginBtn.onclick = function(){
		showLoginBox();
	}
	oLoginClose.onclick = function(){
		hideLoginBox();
	}
	setMoveEvent(oLoginBox);
/*=================登录状态检测=================*/
	if(isLogined()){
		/*setCookie("img_url","img/people.jpg");*/
		logined();
	}
	function showLoginBox(){
		oLoginBox.style.display = "block";
	}
	function hideLoginBox(){
		oLoginBox.style.display = "none";
	}

	//登录成功

	function logined(){
		oLoginBox.parentNode.removeChild(oLoginBox);
		userarea.removeChild(oLoginBtn);
		userarea.removeChild(registerBtn);
		oNews.style.display = "block";
		oUserhead.style.display = "block";
		oUserhead.getElementsByTagName("img")[0].src = getCookie("img_url"); //头像路径

	}